package com.example.victory.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.victory.config.ServiceError;
import com.example.victory.swagger.handler.AppCustomerOverallLabelApiDelegate;
import com.example.victory.swagger.model.*;
import com.example.victory.utils.JsonUtils;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiRequesFsdp;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiRequesFsdpData;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiResponseFsdp;
import com.cgws.fintech.sdk.api.inner.security.model.User;
import com.cgws.fintech.sdk.common.exception.GlobalException;
import com.example.victory.common.web.ServiceHelper;
import com.cgws.fintech.sdk.inner.utils.web.SessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class AppCustomerOverAllLabel implements AppCustomerOverallLabelApiDelegate {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private InnerOverAllLabelFsdpService innerOverAllLabelFsdpService;

    private String getCustomerId(ApiRequestLabelHand param){
        ServiceHelper.checkDataNotNull(param.getData(),"param.data");
        ServiceHelper.checkDataNotNull(param.getData().getCustomerId(),"param.data.customerId");
        return  param.getData().getCustomerId();
    }

    private String getCustomerAccount(){
        User user = sessionService.getCurrentUserSession(true).getUser();
        String crmId = user.getCrmId();
        if (StringUtils.isEmpty(crmId)) {
            throw new GlobalException(ServiceError.ACCOUNT_NOT_EXISTS);
        }
        return crmId;
    }

    @Override
    public ResponseEntity<ApiResponseNull> addCustomerOverAllLabelHand(ApiRequestLabelHand param) {
        ServiceHelper.checkDataNotNull(param.getData().getLabel(),"data.label");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId",getCustomerId(param));
        jsonObject.put("accountId",getCustomerAccount());
        jsonObject.put("label",param.getData().getLabel());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity=null;
        try {
            fsdpResponseEntity=innerOverAllLabelFsdpService.addCustomerOverAllLabelHand(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        }catch (Exception e){
            log.error("请求CRMAPI错误,e={}",e);
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL);
        }
        if(fsdpResponseEntity.getStatusCode()!=HttpStatus.OK||!String.valueOf(fsdpResponseEntity.getBody().getCode()).equals("0")){
            log.error("请求crmapi有关事务的操作,错误信息={}", JSON.toJSON(fsdpResponseEntity));
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL_MESSAGE,fsdpResponseEntity.getBody().getMessage());
        }
        return ResponseEntity.ok(new ApiResponseNull());
    }

    @Override
    public ResponseEntity<ApiResponseNull> deleteCustomerOverAllLabelHand(ApiRequestLabelHand param) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId",getCustomerId(param));
        jsonObject.put("accountId",getCustomerAccount());
        jsonObject.put("label",param.getData().getLabel());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity=null;
        try {
            fsdpResponseEntity=innerOverAllLabelFsdpService.deleteCustomerOverAllLabelHand(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        }catch (Exception e){
            log.error("请求CRMAPI错误,e={}",e);
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL);
        }
        if(fsdpResponseEntity.getStatusCode()!=HttpStatus.OK||!String.valueOf(fsdpResponseEntity.getBody().getCode()).equals("0")){
            log.error("请求crmapi有关事务的操作,错误信息={}", JSON.toJSON(fsdpResponseEntity));
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL_MESSAGE,fsdpResponseEntity.getBody().getMessage());
        }
        return ResponseEntity.ok(new ApiResponseNull());
    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllLabel> getCustomerOverAllLabelHand(ApiRequestCustomerId param) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId",param.getData().getCustomerId());
        jsonObject.put("accountId",getCustomerAccount());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity=innerOverAllLabelFsdpService.getCustomerOverAllLabelHand(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllLabelData hand= JsonUtils.getResultEntity(fsdpResponseEntity,ApiResponseCustomerOverAllLabelData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllLabel().data(hand));
    }
}
